package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 队伍实体类
 * 
 * 对应数据库表：teams
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "teams", 
       indexes = {
           @Index(name = "idx_leader_id", columnList = "leader_player_id"),
           @Index(name = "idx_team_level", columnList = "team_level")
       })
@EntityListeners(AuditingEntityListener.class)
public class Team {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "team_id")
    private Long teamId;
    
    @Column(name = "team_name", nullable = false, length = 50)
    private String teamName;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "leader_player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_team_leader"))
    @JsonIgnore
    private Player leaderPlayer;
    
    @Column(name = "max_members", nullable = false)
    private Integer maxMembers = 5;
    
    @Column(name = "current_members", nullable = false)
    private Integer currentMembers = 1;
    
    @Column(name = "team_level", nullable = false)
    private Integer teamLevel = 1;
    
    @Column(name = "team_score", nullable = false)
    private Integer teamScore = 0;
    
    @Column(name = "description", length = 200)
    private String description;
    
    @Column(name = "is_active", nullable = false)
    private Boolean isActive = true;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    @Column(name = "disband_at")
    private LocalDateTime disbandAt;
    
    // 关联实体
    @OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<TeamMember> members;
    
    @OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<TeamInvitation> invitations;
    
    // 构造函数
    public Team() {}
    
    public Team(String teamName, Player leaderPlayer) {
        this.teamName = teamName;
        this.leaderPlayer = leaderPlayer;
        this.currentMembers = 1;
    }
    
    // Getter和Setter方法
    public Long getTeamId() {
        return teamId;
    }
    
    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }
    
    public String getTeamName() {
        return teamName;
    }
    
    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
    
    public Player getLeaderPlayer() {
        return leaderPlayer;
    }
    
    public void setLeaderPlayer(Player leaderPlayer) {
        this.leaderPlayer = leaderPlayer;
    }
    
    public Integer getMaxMembers() {
        return maxMembers;
    }
    
    public void setMaxMembers(Integer maxMembers) {
        this.maxMembers = maxMembers;
    }
    
    public Integer getCurrentMembers() {
        return currentMembers;
    }
    
    public void setCurrentMembers(Integer currentMembers) {
        this.currentMembers = currentMembers;
    }
    
    public Integer getTeamLevel() {
        return teamLevel;
    }
    
    public void setTeamLevel(Integer teamLevel) {
        this.teamLevel = teamLevel;
    }
    
    public Integer getTeamScore() {
        return teamScore;
    }
    
    public void setTeamScore(Integer teamScore) {
        this.teamScore = teamScore;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public Boolean getIsActive() {
        return isActive;
    }
    
    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public LocalDateTime getDisbandAt() {
        return disbandAt;
    }
    
    public void setDisbandAt(LocalDateTime disbandAt) {
        this.disbandAt = disbandAt;
    }
    
    public List<TeamMember> getMembers() {
        return members;
    }
    
    public void setMembers(List<TeamMember> members) {
        this.members = members;
    }
    
    public List<TeamInvitation> getInvitations() {
        return invitations;
    }
    
    public void setInvitations(List<TeamInvitation> invitations) {
        this.invitations = invitations;
    }
    
    // 业务方法
    public void addScore(int score) {
        this.teamScore += score;
        // 可以根据分数更新队伍等级
        updateLevelByScore();
    }
    
    private void updateLevelByScore() {
        // 简单的等级计算：每1000分升1级
        int newLevel = Math.min(100, 1 + teamScore / 1000); // 最高100级
        if (newLevel > teamLevel) {
            this.teamLevel = newLevel;
        }
    }
    
    public boolean isFull() {
        return currentMembers >= maxMembers;
    }
    
    public boolean isEmpty() {
        return currentMembers <= 0;
    }
    
    public void incrementMemberCount() {
        this.currentMembers++;
    }
    
    public void decrementMemberCount() {
        this.currentMembers = Math.max(0, currentMembers - 1);
    }
    
    public void disband() {
        this.isActive = false;
        this.disbandAt = LocalDateTime.now();
    }
    
    public double getMemberFullness() {
        if (maxMembers == 0) return 0;
        return (double) currentMembers / maxMembers;
    }
    
    @Override
    public String toString() {
        return "Team{" +
                "teamId=" + teamId +
                ", teamName='" + teamName + '\'' +
                ", leaderPlayer=" + (leaderPlayer != null ? leaderPlayer.getPlayerId() : null) +
                ", currentMembers=" + currentMembers +
                ", maxMembers=" + maxMembers +
                ", teamLevel=" + teamLevel +
                ", teamScore=" + teamScore +
                ", isActive=" + isActive +
                ", createdAt=" + createdAt +
                '}';
    }
}